package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try{
            PageResult pageResult = permissionService.findPage(queryPageBean);
            return new Result(true,"查询权限数据成功",pageResult);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询权限数据失败");
        }
    }

    @RequestMapping("/changeStatus.do")
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result changeStatus(@RequestBody Permission permission){
        try{
            permissionService.update(permission);
            return new Result(true,"修改权限状态成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改权限状态失败");
        }
    }

    @RequestMapping("/edit.do")
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result edit(@RequestBody Permission permission){
        try{
            permissionService.update(permission);
            return new Result(true,"编辑权限数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"编辑权限数据失败");
        }
    }

    @RequestMapping("/add.do")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    public Result add(@RequestBody Permission permission){
        try{
            permissionService.add(permission);
            return new Result(true,"添加权限数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加权限数据失败");
        }
    }

    @RequestMapping("/delete.do")
    @PreAuthorize("hasAuthority('PERMISSION_DELETE')")
    public Result delete(Integer id){
        try{
            permissionService.delete(id);
            return new Result(true,"删除权限数据成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除权限数据失败");
        }
    }

    //遍历权限信息
    @RequestMapping("/findAll.do")
    public Result findAll(){
        try{
            List<Permission> list=permissionService.findAll();
            return new Result(true,"查询权限数据成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询权限数据失败");
        }
    }

    //根据id查询指定角色的权限信息
    @RequestMapping("/findByPerIds.do")
    public Result findByPerIds(Integer id){
        try{
            List<Permission> PermissionList=permissionService.findByPerIds(id);
            List<Integer> list=new ArrayList<>();
            for (Permission  p: PermissionList) {
                Integer roleId = p.getId();
                list.add(roleId);
            }
            return new Result(true,"查询权限数据成功",list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询权限数据失败");
        }
    }
}
